package com.orm.analysis.dao;

import com.orm.analysis.model.Customer;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.List;


public class CustomerDAO extends GenericJPADAO<Customer, Integer> {

    /**
     * List all the authors order by lastName,firstName ascendent
     *
     * @param page the page number
     * @param size the page size
     * @return a list of authors
     */
    public List<Customer> list(int page, int size) {
        Query query = this.em.createQuery("from Customer");
        query.setFirstResult((page - 1) * size);
        query.setMaxResults(size);
        return query.getResultList();
    }


    public Customer getById(Integer id) {
            return this.em.find(Customer.class,id);
    }

    @Transactional
    @Override
      public void save(Customer entity) {
          getEntityManager().persist(entity);
      }

    @Transactional
    @Override
        public void delete(Object id) {
            getEntityManager().remove(getEntityManager().find(Customer.class, id));

         }

}
